package newPan3d
{
	import _Pan3D.base.Focus3D;
	import _Pan3D.base.Object3D;
	import _Pan3D.core.Groundposition;
	import _Pan3D.core.Mathclass;
	import _Pan3D.role.Hero;
	import _Pan3D.role.Npc;
	
	import _me.Scene_data;
	
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Vector3D;
	import flash.utils.getTimer;
	
	import newPan3d.content.ground.map.MapClass;
	import newPan3d.content.npc.NpcMenager;
	import newPan3d.ui.UiView;


	public class NewKeyControl extends EventDispatcher
	{
		private var _stage:Stage;
		private var _isFrist:Boolean=true;
		private var _keyobj:Object=new Object;

		public function NewKeyControl()
		{
		}

		public function init(temp_stage:Stage):void
		{
			_stage=temp_stage;
			_stage.addEventListener("keyDown", keydownHandler);
			_stage.addEventListener("keyUp", keyupHandler);

			_stage.addEventListener(MouseEvent.MOUSE_DOWN, stageMouseDown);
			_stage.addEventListener(MouseEvent.MOUSE_WHEEL, mouseWheel);
			_stage.addEventListener(Event.ENTER_FRAME,onEnterFrame)

		}
		
		protected function onEnterFrame(event:Event):void
		{
			if(Scene_data.focus3D){
				if(_keyobj[37]){
					Scene_data.focus3D.angle_y++
				}
				if(_keyobj[39]){
					Scene_data.focus3D.angle_y--
				}
				if(_keyobj[38]){
					Scene_data.focus3D.angle_x++
				}
				if(_keyobj[40]){
					Scene_data.focus3D.angle_x--
				}
			}
			
		}
		
		private function stageMouseDown(event:MouseEvent):void
		{
			if (!(event.target as Stage))
			{
				return;
			}
			var _E:Object3D=Groundposition._getposition(Scene_data.cam3D, Scene_data.stage.mouseX - Scene_data.stage3D.x, Scene_data.stage.mouseY - Scene_data.stage3D.y);

			var beginX:int=Scene_data.focus3D.x;
			var beginZ:int=Scene_data.focus3D.z;
			var endX:int=Scene_data.cam3D.x + _E.x;
			var endZ:int=Scene_data.cam3D.z + _E.z;
			var mapSpeed:int=1000 / 200; //1000为表示地面的尺寸/200表示地图像素

			beginX=beginX / 5
			beginZ=beginZ / 5
			endX=endX / 5
			endZ=endZ / 5

			if (false)
			{
				beginX=100;
				beginZ=100;
				endX=190;
				endZ=190;
			}
			endX=endX > 198 ? 198 : endX;
			endZ=endZ > 198 ? 198 : endZ;
			endX=endX < 1 ? 1 : endX;
			endZ=endZ < 1 ? 1 : endZ;


			var npc:Npc=NpcMenager.findClikNpc(0, 0);

			var hero:Hero=Scene_data.self3DMove;

			if (npc)
			{
				hero.minDistance=20;
			}
			else
			{
				hero.minDistance=1;
					//hero.state = null;
			}

			hero.target=npc;
			if (hero.pathEndX == endX && hero.pathEndZ == endZ)
			{
				return;
			}

			var t:int=getTimer();
			var roadArr:Vector.<Vector3D>=MapClass.getFindLoadArr(beginX, beginZ, endX, endZ);

			Scene_data.self3DMove.pushLoadArr(roadArr);
			if (Scene_data.root.uiView && UiView(Scene_data.root.uiView).mapView)
			{
				UiView(Scene_data.root.uiView).mapView.drawLoadLine(roadArr)
			}
			hero.pathEndX=endX;
			hero.pathEndZ=endZ;

		}
		/*
		private function getNearbyPoint(beginX:Number, beginZ:Number, endX:Number, endZ:Number):Point{
			var angly:Number = Mathclass.math_angle(endX, endX, beginX, beginZ)
			var distance:Number = 3;
			endX -= distance * Math.cos(angly * Math.PI / 180);
			endZ -= distance * Math.sin(angly * Math.PI / 180);
			return new Point(endX,endZ);
		}*/

		private var seepNum:Number=3;


		private function mouseWheel(evt:MouseEvent):void
		{
			Scene_data.cam3D.distance=Scene_data.cam3D.distance + evt.delta * 10;
		}

		private function keydownHandler(evt:KeyboardEvent):void
		{
		
			_keyobj[evt.keyCode]=true;
			_isFrist=false;
			if (evt.keyCode == 87)
			{
				this.dispatchEvent(evt);
			}
			else if (evt.keyCode == 32)
			{
				Scene_data.self3DMove.attack("attack");
			}
			else if (evt.keyCode == 65)
			{
				Scene_data.self3DMove.attack("attack2");
			}

		}

		private function keyupHandler(evt:KeyboardEvent):void
		{
			delete _keyobj[evt.keyCode];
			if (evt.keyCode == 87)
			{
				this.dispatchEvent(evt);
			}

		}

		public function isDown(key:Number):Boolean
		{
			return _keyobj[key] ? true : false;
		}
	}
}
